<?
error_reporting(E_ALL);
$relPath="./../../pinc/";
// include_once($relPath.'site_vars.php');
require_once $relPath."dpinit.php";
include_once($relPath.'dp_main.inc');
// include_once($relPath.'misc.inc');
// include_once($relPath.'theme.inc');
// include_once($relPath.'rounds.php');
include_once($relPath."DpTable.class.php");
// include_once($relPath.'Project.inc');
require_once $relPath."DpDate.class.php";

$projectid = ArgProjectId();
$username  = $User->Username();

$project = new DpProject( $projectid );
$roundid = $project->RoundId();

$page_details_str = _('Page Details');
$title            = $project->Title();
$url              = "$code_url/project.php?projectid=$projectid" ;
$label            = _("Return to Project Page");

$no_stats = 1;
$args = array() ;
// $args['css_data'] = mycss() ;
theme( "$page_details_str: $title", 'header', $args );
echo "</table>\n";

echo "<h1>$title</h1>\n";
// echo "<h2>$page_details_str</h2>\n";


echo "<p><a href='$url'>$label</a></p>\n";
echo "<br>\n";

$roundindex = RoundIndexForId($roundid);

$tbl = new DpTable();

$tbl->AddColumn( _( "Page Code" ), null, "_page_name" );

foreach(RoundIdsInOrder() as $roundid)
{
    if(RoundIndexForId($roundid) > $roundindex)
        break;

    if(RoundIndexForId($roundid) == 1)
    {
        $tbl->AddColumn( "OCR->P1", null, "_diff_1" ) ;
    }
    else
    {
        $i = RoundIndexForId($roundid);
        $prev = RoundIdAfter($roundid);
        $tbl->AddColumn( "{$roundid}->{$prev}", null, "_diff_{$i}" ) ;
    }
}

$rows = _fetchObjects( $projectid ) ;
$tbl->SetRows( $rows ) ;
$tbl->EchoTableNumbered() ;

echo "<br>";
theme( '', 'footer' );

function _fetchObjects( $projectid ) 
{
    global $dpdb;
    $sql = sql( $projectid ) ;
    $rows = $dpdb->SqlObjects($sql);
    return $rows;
}

function sql( $projectid )
{
    return "SELECT
                '$projectid' AS projectid,
                fileid AS pagename,
                state,
                round1_user,
                round2_user,
                round3_user,
                round4_user,
                round5_user,
                round6_user,
                round7_user,
                round8_user,
                round9_user,
                round10_user,
                round1_time,
                round2_time,
                round3_time,
                round4_time,
                round5_time,
                round6_time,
                round7_time,
                round8_time,
                round9_time,
                round10_time,
                ( master_text != BINARY round1_text ) AS is_diff1,
                ( round1_text != BINARY round2_text ) AS is_diff2,
                ( round2_text != BINARY round3_text ) AS is_diff3,
                ( round3_text != BINARY round4_text ) AS is_diff4,
                ( round4_text != BINARY round5_text ) AS is_diff5,
                ( round5_text != BINARY round6_text ) AS is_diff6,
                ( round6_text != BINARY round7_text ) AS is_diff7,
                ( round7_text != BINARY round8_text ) AS is_diff9,
                ( round8_text != BINARY round9_text ) AS is_diff9,
                ( round9_text != BINARY round10_text ) AS is_diff10
            FROM
                pages
            WHERE
                projectid = '$projectid'
            ORDER BY
                pagename" ;
}

function _page_name( $row )
{
    return $row->pagename ;
}

function _state( $row )
{
    return $row->state ;
}

function _proofer_time( $username, $ts )
{
    global $pguser ;

    if( $username && $ts )
    {
        $d = new DpDate( $ts ) ;
        $ret =  "$username ("
                 .$d->PrettyDateTime()
                 .")\n" ;
        if( $pguser == $username )
            $ret = "<span class='me'>$ret</span>\n";
    }
    else
        $ret = "" ;
    return $ret ;
}

function _proofer_time_1( $row )
{
    return _proofer_time( $row->round1_user, $row->round1_time ) ;
}

function _proofer_time_2( $row )
{
    return _proofer_time( $row->round2_user, $row->round2_time ) ;
}

function _proofer_time_3( $row )
{
    return _proofer_time( $row->round3_user, $row->round3_time ) ;
}

function _proofer_time_4( $row )
{
    return _proofer_time( $row->round4_user, $row->round4_time ) ;
}

function _proofer_time_5( $row )
{
    return _proofer_time( $row->round5_user, $row->round5_time ) ;
}

function _diff( $row, $rnum )
{
    switch( $rnum )
    {
        default:
            $isdiff = 0 ;
        case 1:
            $isdiff = $row->is_diff1 ;
            break ;
        case 2:
            $isdiff = $row->is_diff2 ;
            break ;
        case 3:
            $isdiff = $row->is_diff3 ;
            break ;
        case 4:
            $isdiff = $row->is_diff4 ;
            break ;
        case 5:
            $isdiff = $row->is_diff5 ;
            break ;
    }
    $dmsg = ( $isdiff == 0 ) ?  "Identical" : "Diff!!" ;
    return "<a href='diffview.php"
            . "?projectid=" . $row->projectid
            . "&amp;pagename=" . $row->pagename
            . "&amp;roundid=".$rnum."'>"
            . $dmsg
            . "</a>\n";
}

function _diff_1( $row )
{
    if( empty( $row->round1_user ) )
        return "" ;
    else
        return _diff( $row, 1 ) ;
    /*
    return "<a href='diffview.php"
            . "?projectid=" . $row->projectid
            . "&amp;pagename=" . $row->pagename
            . "&amp;roundid=1'>"
            . _diff( $row->is_diff1 )
            . "</a>\n";
    */
}

function _diff_2( $row )
{
    if( empty( $row->round2_user ) )
        return "" ;
    else
        return _diff( $row, 2 ) ;
}

function _diff_3( $row )
{
    if( empty( $row->round3_user ) )
        return "" ;
    else
        return _diff( $row, 3 ) ;
}

function _diff_4( $row )
{
    if( empty( $row->round4_user ) )
        return "" ;
    else
        return _diff( $row, 4 ) ;
}

function _diff_5( $row )
{
    if( empty( $row->round5_user ) )
        return "" ;
    else
        return _diff( $row, 5 ) ;
}

function _diff_click( $row )
{
    return "Diff Click" ;
    return _diff( $row, 1 ) ;
}

/*
function _timestamp( $row )
{
    return _daysAgo( $row->max_round_timestamp ) . _( " days ago" ) ;
}
*/

function mycss()
{
    return "
    .me {
       background-color: black;
        color: white ;
    }\n";
}

// vim: sw=4 ts=4 expandtab
?>

